//
//  LKEmoticonBottomView.swift
//  LKWeibo
//
//  Created by like on 16/8/18.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit

// 定义一个参数
private let tagNumber = 200

// 按钮枚举
enum LKEmoticonBottomViewType: Int {
    // 默认
    case Default = 0
    // emoji
    case Emoji = 1
    // 浪小花
    case Lxh = 2
}

//UIStackView 容器视图 ios 9.0 以上可以使用
class LKEmoticonBottomView: UIStackView {

    // 当前选中的按钮
    var currentButton: UIButton?
    //定义一个闭包  01
    var colseure: ((type: LKEmoticonBottomViewType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
//        backgroundColor = RandomColor()
        
        // 填充方向
        axis = .Horizontal
        // 填充方式
        distribution = .FillEqually
        
        //创建button
        addChildButtons("left", title: "默认",type: .Default)
        addChildButtons("mid", title: "Emoji",type: .Emoji)
        addChildButtons("right", title: "浪小花",type: .Lxh)
        
//        //创建button
//        let defaultButton = addChildButtons("left", title: "默认",type: .Default)
//        let emojiButton = addChildButtons("mid", title: "Emoji",type: .Emoji)
//        let lxhButton = addChildButtons("right", title: "浪小花",type: .Lxh)
//        //添加约束
//        defaultButton.snp_makeConstraints { (make) in
//            make.top.leading.bottom.equalTo(self)
//            make.width.equalTo(emojiButton)
//        }
//        
//        emojiButton.snp_makeConstraints { (make) in
//            make.top.bottom.equalTo(self)
//            make.leading.equalTo(defaultButton.snp_trailing)
//            make.width.equalTo(lxhButton)
//            
//        }
//        
//        lxhButton.snp_makeConstraints { (make) in
//            make.top.bottom.trailing.equalTo(self)
//            make.leading.equalTo(emojiButton.snp_trailing)
//        }
        
    }
    
    
// MARK: - 提供方法 供外界调用
    func setupCurrentButton(tag: Int) {
        // 得到button
        let button = viewWithTag(tag + tagNumber) as! UIButton
        selectButton(button)
    }
    
    // 选中设置判断
    func selectButton(button: UIButton){
        // 如果当前选中的button和这次的相同将不再设置
        if currentButton == button {
            return
        }
        // 取消上个选中的按钮
        currentButton?.selected = false
        // 把当前的选中
        button.selected = true
        // 赋值
        currentButton = button
    }
    
    
    //三个button按钮 监听的事件
    func buttonClick(sender:UIButton){
        
        selectButton(sender)
        //闭包 03
        colseure?(type: LKEmoticonBottomViewType(rawValue: sender.tag - tagNumber)!)
    }
    
    
    //添加三个button
    private func addChildButtons(imgName: String, title: String, type: LKEmoticonBottomViewType)->UIButton{
        let button = UIButton()
        button.tag = type.rawValue + tagNumber
        // 添加事件
        button.addTarget(self, action: #selector(LKEmoticonBottomView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_selected"), forState: UIControlState.Selected)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
//        addSubview(button)
        addArrangedSubview(button)
        
        if type == .Default {
            button.selected = true
            currentButton = button
        }
        return button
    }
    
}
